//
//  HLPreviewGiftView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/13.
//

#import "HLPreviewGiftView.h"
#import <FrameAccessor/FrameAccessor.h>

@interface HLPreviewGiftView ()

@property (strong,  nonatomic) UIImageView *imageView;
@property (weak,    nonatomic) UIView *onView;

@end

@implementation HLPreviewGiftView

- (void)showGift:(HLGiftAttachment *)gift onView:(UIView *)onView;
{
    self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
    self.onView = onView;
    self.frame = onView.bounds;
    [self addGestureRecognizer:[UITapGestureRecognizer.alloc initWithTarget:self action:@selector(tapEvent)]];
    [onView addSubview:self];
    [self createImageWithGift:gift];
    
}

- (void)createImageWithGift:(HLGiftAttachment *)gift
{
    UIImageView *imageView = [UIImageView.alloc initWithFrame:CGRectMake(0, 0, self.width * 0.7, self.width * 0.7)];
    imageView.center = self.middlePoint;
    [imageView sd_setImageWithURL:[NSURL URLWithString:gift.giftImgLink] placeholderImage:[UIImage imageNamed:@""]];
    [self addSubview:imageView];
    self.imageView = imageView;
    
    self.imageView.alpha = 0.0;
    self.imageView.transform = CGAffineTransformMakeScale(0.2, 0.2);
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.imageView.alpha = 1.0;
        self.imageView.transform = CGAffineTransformIdentity;
    } completion:nil];
}



- (void)tapEvent
{
    [UIView animateWithDuration:0.15 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.imageView.alpha = 0.0;
        self.imageView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    } completion:^(BOOL finished) {
        [self.imageView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


@end
